//
//  ProveViewController.m
//  Hxcsfw
//
//  Created by qiysoft on 2018/2/1.
//  Copyright © 2018年 企友软mac. All rights reserved.
//

#import "ProveViewController.h"
#import "ProveCell.h"
#define ProveCellIndentifier @"ProveCellIndentifier"
#import "CodeView.h"

@interface ProveViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    ProveCell *cell1;
}

@end

@implementation ProveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"注册";
    
    // 1.注册控件
    UINib *cellNib = [UINib nibWithNibName:@"ProveCell" bundle:nil];
    [self.myTableView registerNib:cellNib forCellReuseIdentifier:ProveCellIndentifier];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.prototypeCell  = [self.myTableView dequeueReusableCellWithIdentifier:ProveCellIndentifier];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PhoneRegistSuccessClick) name:@"PhoneRegistSuccess" object:nil];

}

- (void)PhoneRegistSuccessClick {//手机注册成功后，自动走验证码登录接口
    
    [self requestLogin:self.strPhone code:[WShareUtil sharedData].RegistSMSyzm];

//    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//设置UITableViewCell的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell1  = [[[NSBundle mainBundle] loadNibNamed:@"ProveCell" owner:self options:nil] lastObject];
    cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    cell1.phoneLB.text = [NSString stringWithFormat:@"%@",self.strPhone];
    
    //验证码框
    CodeView *v = [[CodeView alloc] initWithFrame:CGRectMake(0, 0, WSCREENH_WIDTH - 38-38, 30)
                                              num:6
                                        lineColor:[UIColor blackColor]
                                         textFont:30];
    //下划线
    v.hasUnderLine = YES;
    //分割线
    v.hasSpaceLine = NO;
    //下划线闪动
    v.underLineAnimation = YES;
    //输入风格
    v.codeType = CodeViewTypeCustom;
    v.EndEditBlcok = ^(NSString *str) {
        WLog(@"注册验证码：%@",str);
        [WShareUtil sharedData].RegistSMSyzm = str;
        //注册短信验证码存单例
    };
    [cell1.YZMView addSubview:v];
    
    return cell1;
    
}

//验证码框代理回调
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

//行点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

//分组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//列表行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

//设置行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return WSCREENH_HEIGHT-72-49;
    
}

#pragma mark - 验证码登录请求
- (void)requestLogin:(NSString *)phone code:(NSString *)code{
    
    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"shortlogin",@"content":@{@"revert":@"login",@"phone":phone,@"code":code}};
    
    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};
    
    
    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];
        
        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];
        
        if ([successStr boolValue]) {
            //            if (self.delegate && [self.delegate respondsToSelector:@selector(btnOfVerifyLoginClicked)]) {
            //                [self.delegate btnOfVerifyLoginClicked];
            //            }
            [SVProgressHUD showImage:nil status:@"登录成功"];
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
            [SVProgressHUD dismissWithDelay:1.];
            [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];   //背景颜色
            [WShareUtil sharedData].dicSession = returnDic;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:nil];

        } else {
            [WShareUtil showAlert:@"温馨提示" message:@"短信验证码错误！请重新输入"];
        }
        
    } failure:^(NSError *error) {
        [SVProgressHUD showImage:nil status:@"登录失败"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
        [SVProgressHUD dismissWithDelay:1.];
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];
        
    } showProgress:YES];
}

@end
